var setting;
var log, className = "dark";
var ztree;
var selNode;
$(function() {
	// 构造设备树结构
	setting = {
		data : {
			simpleData : {
				enable : true,
				idKey : "id",
				rootPId:null
			}
		},
		callback : {
			onClick : zTreeOnClick,
			onRightClick:OnRightClick
		}
	};
})

/**
 * 查询详细分段
 * 
 * @param sid
 */
function queryDetail(sid){
	var d=$("#"+sid);
	d.empty();
	if(d.is(':hidden')){
		d.show();
	}else{
		d.hide();
	}
	$.ajax({
		type : "POST",
		url : "../../businessSense/subsectionManage/queryDetail?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"sid" : sid
		},
		success : function(data) {
			var l=data.list;
			$.each(l, function(m, comment) {
				if(comment['subsection_type']=="2"){
					d.append("<div style='font-size: 15px;'>"+comment['subsection_subd_name']+"：</div>");
					$.each(l, function(n, detail) {
						if(detail['subsection_id']==comment['id']){
							d.append("<div>"+detail['subsection_subd_name']+"：<span id=''>"+detail['min_value']+"</span>至<span id=''>"+detail['max_value']+"</span>ms" +
												"<span style='background:"+detail['color']+";'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" +
												"</div>");
						}
					});
					d.append("<hr>");
				}
			});
		}
	});
}

/**
 * 弹出管理界面
 */
function forwordSubManage(){
	
	$("#subsection_subd_name").hide();
	$("#subsection_subd_title").hide();
	$("#min_value").hide();
	$("#max_value").hide();
	$("#unit").hide();
	$("#color").hide();
	
	$("#subsection_subd_name_title").hide();
	$("#subsection_subd_title_title").hide();
	$("#min_value_title").hide();
	$("#max_value_title").hide();
	$("#unit_title").hide();
	$("#color_title").hide();
	
	$("#saveBtn").hide();
	$("#addBtn").hide();
	$("#closeBtn").hide();
	
	$.ajax({
		type : "POST",
		url : "../../businessSense/subsectionManage/getTreeData?rand="
				+ Math.random(),
		dataType : "json",
		success : function(data) {
			ztree=$.fn.zTree.init($("#treeDemom"), setting,data);			
			layer.open({
				title:'分段管理',
			    type: 1,
			    area: ['600px','400px'],
			    content:$('#subManage')
			});
		}
	});
}

/**
 * 新增分段
 */
function addSubsection(){
	var subsection_subd_name=$("#subsection_subd_name").val();
	var subsection_subd_title=$("#subsection_subd_title").val();
	var min_value=$("#min_value").val();
	var max_value=$("#max_value").val();
	var unit=$("#unit").val();
	var color=$("#color").val();
	var type=selNode.level+1;
	var pid=selNode.id
	
	$.ajax({
		type : "POST",
		url : "../../businessSense/subsectionManage/addSubsection?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"pid" : pid,
			"subsection_subd_name" : subsection_subd_name,
			"subsection_subd_title" : subsection_subd_title,
			"min_value" : min_value,
			"max_value" : max_value,
			"unit" : unit,
			"color" : color,
			"type" : type
		},
		success : function(data) {
			if(data.flag){
				ztree.addNodes(selNode,data.newNode);
				layer.alert(data.msgs, {
					icon : 1
				});
			}else{
				layer.alert(data.msgs, {
					icon : 3
				});
			}
		}
	});
}

/**
 * 修改分段信息
 */
function updateSubsection(){
	var id=$("#id").val();
	var subsection_subd_name=$("#subsection_subd_name").val();
	var subsection_subd_title=$("#subsection_subd_title").val();
	var min_value=$("#min_value").val();
	var max_value=$("#max_value").val();
	var unit=$("#unit").val();
	var color=$("#color").val();
	
	$.ajax({
		type : "POST",
		url : "../../businessSense/subsectionManage/updateSubsection?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"sid" : id,
			"subsection_subd_name" : subsection_subd_name,
			"subsection_subd_title" : subsection_subd_title,
			"min_value" : min_value,
			"max_value" : max_value,
			"unit" : unit,
			"color" : color
		},
		success : function(data) {
			if(data.flag){
				selNode.name=data.nodeName;
				ztree.updateNode(selNode);
				layer.alert(data.msgs, {
					icon : 1
				});
			}else{
				layer.alert(data.msgs, {
					icon : 3
				});
			}
		}
	});
}

function closeWin(){
	layer.closeAll();
	location.href = "../../businessSense/subsectionManage/index?number="+Math.random();
}

/**
 * 树点击事件
 * @param event
 * @param treeId
 * @param treeNode
 */
function zTreeOnClick(event, treeId, treeNode){
	selNode=treeNode;
	var l=treeNode.level;
	dataDisplay(l);
	$.ajax({
		type : "POST",
		url : "../../businessSense/subsectionManage/queryById?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"sid" : treeNode.id
		},
		success : function(data) {
			$("#subsection_subd_name").val(data.result.subsection_subd_name);
			$("#subsection_subd_title").val(data.result.subsection_subd_title);
			$("#min_value").val(data.result.min_value);
			$("#max_value").val(data.result.max_value);
			$("#unit").val(data.result.unit);
			$("#color").val(data.result.color);
			
			$("#id").val(data.result.id);
		}
	});
}

/**
 * 树右键事件
 * @param event
 * @param treeId
 * @param treeNode
 */
function OnRightClick(event, treeId, treeNode) {
	selNode=treeNode;
	ztree.selectNode(treeNode);
	if(treeNode) {
//		console.log(treeNode.tId);
		//console.dir($('#'+treeNode.tId));

		$("#menu").popupSmallMenu({
			event : event,
			onClickItem  : function(item) {
				chuli(treeNode,item);
			}
		});
	}	 
}

function chuli(treeNode,item){
	var href;
	var l=treeNode.level;
	if(item=="edit"){
//		href="../../businessSense/subsectionManage/addSubsection?rand="+ Math.random();
		var subsection_subd_name=$("#subsection_subd_name");
		var subsection_subd_title=$("#subsection_subd_title");
		var min_value=$("#min_value");
		var max_value=$("#max_value");
		var unit=$("#unit");
		var color=$("#color");
		
		var subsection_subd_name_titile=$("#subsection_subd_name_title");
		var subsection_subd_title_titile=$("#subsection_subd_title_title");
		var min_value_titile=$("#min_value_title");
		var max_value_titile=$("#max_value_title");
		var unit_titile=$("#unit_title");
		var color_titile=$("#color_title");
		
		$("#saveBtn").hide();
		$("#addBtn").show();
		$("#closeBtn").show();
		
		if(l==0){
			subsection_subd_name.show();
			subsection_subd_name.val("");
			subsection_subd_title.hide();
			min_value.hide();
			max_value.hide();
			unit.hide();
			color.hide();

			subsection_subd_name_titile.show();
			subsection_subd_title_titile.hide();
			min_value_titile.hide();
			max_value_titile.hide();
			unit_titile.hide();
			color_titile.hide();
		}else if(l==1){
			subsection_subd_name.show();
			subsection_subd_name.val("");
			subsection_subd_title.show();
			subsection_subd_title.val("");
			min_value.hide();
			max_value.hide();
			unit.hide();
			color.hide();

			subsection_subd_name_titile.show();
			subsection_subd_title_titile.show();
			min_value_titile.hide();
			max_value_titile.hide();
			unit_titile.hide();
			color_titile.hide();
		}else if(l==2){
			subsection_subd_name.show();
			subsection_subd_name.val("");
			subsection_subd_title.show();
			subsection_subd_title.val("");
			min_value.show();
			min_value.val("");
			max_value.show();
			max_value.val("");
			unit.show();
			unit.val("");
			color.show();
			color.val("");

			subsection_subd_name_titile.show();
			subsection_subd_title_titile.show();
			min_value_titile.show();
			max_value_titile.show();
			unit_titile.show();
			color_titile.show();
		}else{
			layer.alert("末级节点无法新增分段", {
				icon : 3
			});
		}
	}else if(item=="delete"){
		href="../../businessSense/subsectionManage/deleteSubsection?rand="+ Math.random();
		
		$.ajax({
			type : "POST",
			url : href,
			dataType : "json",
			"data" : {
				"sid" : selNode.id
			},
			success : function(data) {
				if(data.flag){
					ztree.removeNode(selNode);
					layer.alert(data.msgs, {
						icon : 1
					});
				}else{
					layer.alert(data.msgs, {
						icon : 3
					});
				}
			}
		});
	}
}

/**
 * 控制显示影藏
 */
function dataDisplay(level){
	
	var subsection_subd_name=$("#subsection_subd_name");
	var subsection_subd_title=$("#subsection_subd_title");
	var min_value=$("#min_value");
	var max_value=$("#max_value");
	var unit=$("#unit");
	var color=$("#color");
	
	var subsection_subd_name_titile=$("#subsection_subd_name_title");
	var subsection_subd_title_titile=$("#subsection_subd_title_title");
	var min_value_titile=$("#min_value_title");
	var max_value_titile=$("#max_value_title");
	var unit_titile=$("#unit_title");
	var color_titile=$("#color_title");
	if(level==0){
		subsection_subd_name.hide();
		subsection_subd_title.hide();
		min_value.hide();
		max_value.hide();
		unit.hide();
		color.hide();

		subsection_subd_name_titile.hide();
		subsection_subd_title_titile.hide();
		min_value_titile.hide();
		max_value_titile.hide();
		unit_titile.hide();
		color_titile.hide();
		
		$("#saveBtn").hide();
		$("#addBtn").hide();
		$("#closeBtn").hide();
	}else if(level==1){
		subsection_subd_name.show();
		subsection_subd_title.hide();
		min_value.hide();
		max_value.hide();
		unit.hide();
		color.hide();

		subsection_subd_name_titile.show();
		subsection_subd_title_titile.hide();
		min_value_titile.hide();
		max_value_titile.hide();
		unit_titile.hide();
		color_titile.hide();
		
		$("#saveBtn").show();
		$("#addBtn").hide();
		$("#closeBtn").show();
	}else if(level==2){
		subsection_subd_name.show();
		subsection_subd_title.show();
		min_value.hide();
		max_value.hide();
		unit.hide();
		color.hide();

		subsection_subd_name_titile.show();
		subsection_subd_title_titile.show();
		min_value_titile.hide();
		max_value_titile.hide();
		unit_titile.hide();
		color_titile.hide();
		
		$("#saveBtn").show();
		$("#addBtn").hide();
		$("#closeBtn").show();
	}else if(level==3){
		subsection_subd_name.show();
		subsection_subd_title.show();
		min_value.show();
		max_value.show();
		unit.show();
		color.show();
		
		subsection_subd_name_titile.show();
		subsection_subd_title_titile.show();
		min_value_titile.show();
		max_value_titile.show();
		unit_titile.show();
		color_titile.show();
		
		$("#saveBtn").show();
		$("#addBtn").hide();
		$("#closeBtn").show();
	}
}